//
//  FQDSimpleFeedCellItemProvider.m
//  FakeQDaily
//
//  Created by mewxiv on 9/20/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDSimpleFeedCellItemProvider.h"
#import "FQDRegularFeedCellItem.h"
#import "FQDRestModel.h"
#import "YYModel.h"

@interface FQDSimpleFeedCellItemProvider ()

@property (nonatomic, strong) NSArray *responseFeeds;

@end

@implementation FQDSimpleFeedCellItemProvider

- (instancetype)init
{
    self = [super init];
    if (self) {
        _responseFeedsName = @"feeds";
        _responseFeedClass = [FQDFeed class];
        _cellItemClass = [FQDRegularFeedCellItem class];
    }
    return self;
}

- (void)getCellItems:(NSString *)cursorKey completion:(QDailyAPICompleteBlock)completion
{
    if (self.fetchFeedBlock) {
        self.fetchFeedBlock(self.feedId, cursorKey, completion);
    } else {
        FQDLog(@"FQDSimpleFeedCellItemProvider: fetchFeedBlock is nil");
    }
}

- (void)parseResponse:(id)response isLatest:(BOOL)isLatest
{
    self.responseFeeds = [NSArray yy_modelArrayWithClass:self.responseFeedClass
                                             json:response[self.responseFeedsName]];
}

- (void)populateNewCellItemsTo:(NSMutableArray *)originalCellItems
{
    for (id feed in self.responseFeeds) {
        FQDFeedCellItem *cellItem = [self.cellItemClass cellItemWithRestModel:feed];
        [originalCellItems addObject:cellItem];
    }
}

@end